package com.mes.web.controller.biz;

import com.github.pagehelper.PageInfo;
import com.mes.biz.domain.BizCustomer;
import com.mes.biz.domain.BizCustomerOpening;
import com.mes.biz.domain.BizCustomerPayable;
import com.mes.biz.domain.BizCustomerPayableDetail;
import com.mes.biz.mapper.BizCustomerMapper;
import com.mes.biz.service.BizCustomerOpeningService;
import com.mes.biz.service.BizCustomerPayableDetailService;
import com.mes.biz.service.BizCustomerService;
import com.mes.common.annotation.Log;
import com.mes.common.core.controller.BaseController;
import com.mes.common.core.domain.R;
import com.mes.common.core.page.PageDomain;
import com.mes.common.enums.BusinessType;
import com.mes.common.utils.SecurityUtils;
import com.mes.common.utils.file.FileUploadUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 客户应收期初Controller
 * @author zjw
 * @email zjw
 * @date 2023-06-06
 */
@RestController
@RequestMapping("/biz/bizCustomerOpening")
public class BizCustomerOpeningController extends BaseController {

    @Autowired
    private BizCustomerOpeningService bizCustomerOpeningService;

    @Autowired
    private BizCustomerService bizCustomerService;

    /**
     * 对账列表
     */
    @PreAuthorize("@ss.hasPermi('biz:bizCustomerOpening:list')")
    @GetMapping("/list")
    public R<PageInfo> list(BizCustomerOpening bizCustomerOpening, HttpServletRequest request, HttpServletResponse response) {
        bizCustomerOpening.setPage(new PageDomain(request, response));
        PageInfo<BizCustomerOpening> page = bizCustomerOpeningService.findPage(bizCustomerOpening);
        return R.data(page);
    }

    /**
     * 查询所有对客户应收期初列表
     */
    @GetMapping("/allList")
    public R<List<BizCustomerOpening>> list() {
        return R.data(bizCustomerOpeningService.findList(null));
    }

    /**
     * 获取客户应收期初详细信息
     */
    @PreAuthorize("@ss.hasPermi('biz:bizCustomerOpening:query')")
    @GetMapping(value = "/{id}")
    public R<BizCustomerOpening> detail(@PathVariable("id") String id) {
        return R.data(bizCustomerOpeningService.get(id));
    }

    /**
     * 新增客户应收期初
     */
    @PreAuthorize("@ss.hasPermi('biz:bizCustomerOpening:add')")
    @Log(title = "客户应收期初", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody @Validated BizCustomerOpening bizCustomerOpening) {
        BizCustomer bizCustomer = bizCustomerService.get(bizCustomerOpening.getCustomerId());
        bizCustomerOpening.setTenantId(SecurityUtils.getTenantId());
        bizCustomerOpening.setStatus("0");
        bizCustomerOpening.setCustomerName(bizCustomer.getCustomerName());
        return R.status(bizCustomerOpeningService.save(bizCustomerOpening));
    }

    /**
     * 修改客户应收期初
     */
    @PreAuthorize("@ss.hasPermi('biz:bizCustomerOpening:edit')")
    @Log(title = "客户应收期初", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@RequestBody @Validated BizCustomerOpening bizCustomerOpening) {
        BizCustomer bizCustomer = bizCustomerService.get(bizCustomerOpening.getCustomerId());
        bizCustomerOpening.setCustomerName(bizCustomer.getCustomerName());
        return R.status(bizCustomerOpeningService.save(bizCustomerOpening));
    }

    /**
     * 删除客户应收期初
     */
    @PreAuthorize("@ss.hasPermi('biz:bizCustomerOpening:remove')")
    @Log(title = "客户应收期初", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R remove(@PathVariable String[] ids) {
        return R.status(bizCustomerOpeningService.deleteBizCustomerOpeningByIds(ids));
    }

    /**
     * 审核
     */
    @GetMapping(value = "/audit/{id}")
    public R auditOrder(@PathVariable String id) {
        return R.status(bizCustomerOpeningService.auditBizCustomerOpening(id));
    }

    /**
     * 审核
     */
    @GetMapping(value = "/doAudit/{id}")
    public R doAuditOrder(@PathVariable String id) {
        return R.status(bizCustomerOpeningService.doAuditBizCustomerOpening(id));
    }
}
